namespace Brjnk.Maps
{
    using System;
    using System.Windows;
    using Brjnk.Common;

    /// <summary>
    /// Mercatorova projekce.
    /// </summary>
    public class MercatorProjection : IMapProjection
    {
        public GpsCoordinates ToGpsCoordinates(System.Windows.Point internalCoordinates)
        {
            double lat, lon;
            double n = Math.PI - ((2.0 * Math.PI * internalCoordinates.Y));

            lon = (float)((internalCoordinates.X * 360.0) - 180.0);
            lat = (float)(180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
            return new GpsCoordinates(lat, lon);
        }

        public Point ToInternal(GpsCoordinates gps)
        {
            Point retVal = new Point();
            retVal.X = ((gps.LonTotalDegree + 180.0) / 360.0);
            retVal.Y = ((1.0 - Math.Log(Math.Tan(gps.LatTotalDegree * Math.PI / 180.0) +
                1.0 / Math.Cos(gps.LatTotalDegree * Math.PI / 180.0)) / Math.PI) / 2.0);
            return retVal;
        }
    }
}